(function($){
    $.showForm = function(t) {
        var rel = $(t).attr('rel');
        $('.to-toggle').hide();
        if ($('body').hasClass('effect')) {
            $('#' + rel).show("normal");
        }
        else {
            $('#' + rel).show();
        }
    };

    $.hideForm = function(t) {
        if ($('body').hasClass('effect')) {
            $(t).parents('.to-toggle').hide('normal');
        }
        else {
            $(t).parents('.to-toggle').hide();
        }
    }

    $.inputLangChange = function(t) {
        var value = $(t).find('option:selected').attr('value');
        var url = $(t).parents('form').find('input[name=nice_url]').attr('value');
        var data = {
            action: 'input_lang_change',
            lang : value,
            nice_url : url
        }
        var send = $.param(data);
        $.ajax({
            url : 'ajax/course-ajax.php',
            data : send,
            type : 'post',
            dataType: 'json',
            success: function(data) {
                if (data.code == 1) {
                    $(t).parents('form').find('input[name=course_name]').attr('value', data.name);
                    $(t).parents('form').find('textarea[name=course_description]').val(data.description);
                }
            }
        });
    };

    $.fetchCourseDropdown = function(current) {
        var data;
        if (current == undefined) {
            data = {
                action : 'get_course_dropdown'
            }
        }
        else {
            data = {
                action : 'get_course_dropdown',
                nice_url : current
            }
        }
        var send = $.param(data);
        $("#course-dropdown").attr("disabled", "disabled");
        $(".form-button").addClass("disabled");
        $(".form-button").attr("disabled","disabled");
        $("#course-dropdown").parent().find('.ajax-load').show();
        $.ajax({
            url : 'ajax/course-ajax.php',
            type : "POST",
            data : send,
            dataType: 'html',
            success : function(data) {
                $("#course-dropdown").removeAttr('disabled');
                $(".form-button").removeClass("disabled");
                $(".form-button").removeAttr('disabled');
                $("#course-dropdown").parent().find('.ajax-load').hide();
                $("#course-dropdown").html(data);

                $('#course-dropdown').change(function(){
                    $.fetchEditCourseForm();
                });

                $.fetchEditCourseForm();
                $.fetchDocumentsTable(".jtable");
                $.fetchDocumentAddForm();
            },
            error : function() {
                $("#course-dropdown").parent().removeAttr('disabled');
                $(".form-button").removeClass("disabled");
                $(".form-button").removeAttr('disabled');
                $("#course-dropdown").parent().find('.ajax-load').hide();
            }
        });
    }

    $.fetchEditCourseForm = function() {
        var value = $('#course-dropdown option:selected').attr('value');
        var data = {
            action : 'get_course_form',
            nice_url : value
        };
        var send = $.param(data);
        $("#course-dropdown").attr("disabled", "disabled");
        $(".form-button").addClass("disabled");
        $(".form-button").attr("disabled","disabled");
        $("#course-dropdown").parent().find('.ajax-load').show();

        $.ajax({
            url : 'ajax/course-ajax.php',
            type : "POST",
            data : send,
            dataType: 'html',
            success : function(data) {
                $("#course-dropdown").removeAttr('disabled');
                $(".form-button").removeClass("disabled");
                $(".form-button").removeAttr('disabled');
                $("#course-dropdown").parent().find('.ajax-load').hide();
                $("#course-edit-container").html(data, 5000);
                
                $('.form-button[type=reset]').click(function(){
                    $.hideForm(this);
                });

                $(".course-edit-ajax-submit").click(function(){
                    $.submitEditCourseForm(this);
                });

                $('.input-lang-changer').change(function(){
                    $.inputLangChange(this);
                });
            },
            error : function() {
                $("#course-dropdown").removeAttr('disabled');
                $(".form-button").removeClass("disabled");
                $(".form-button").removeAttr('disabled');
                $("#course-dropdown").parent().find('.ajax-load').hide();
            }
        });
    };

    $.fetchDocumentsTable = function(t) {
        var value = $('#course-dropdown option:selected').attr('value');
        var data = {
            action : 'get_documents_table',
            nice_url : value
        };
        
        $(t).jTable({
            url : 'ajax/course-ajax.php',
            params : data,
            onSelectTwice : function(cell) {
                $(cell).removeClass('selectable').addClass('unselectable');
                $(cell).find(".document-name").hide();
                $(cell).find(".document-edit-form").show();
            },
            onSuccess : function() {
                $(t).trigger("update");
                var sorting = [[0,0],[1,0]];
                $(t).trigger("sorton",[sorting]);
                $.formDecorate();
                $('.document-edit-cancel').click(function(){
                    $.hideForm(this);
                    var cell = $(this).parents(".unselectable");
                    $(this).parents(".unselectable").find(".document-name").show();
                    $(this).parents(".unselectable").removeClass('unselectable').addClass('selectable');
                    $('.jtable').jTableBindEventFor(cell);
                });
                $(".document-edit-ajax-submit").click(function(){
                    return $.submitDocumentEditForm(this);
                });
            }
        });
    };

    $.fetchDocumentAddForm = function() {
        var value = $('#course-dropdown option:selected').attr('value');
        $("#document-add input[name=nice_url]").attr('value', value);
    }

    $.submitAddCourseForm = function(t) {
        return $.submitForm(t, function(form, data){
            if (data.code == 1) {
                $.fetchCourseDropdown();
                $(form).find('.ajax-message').htmlTimer(data.message, 5000);
                $(form).resetForm();
            }
            else {
                $(form).find('.ajax-message').htmlTimer(data.message, 5000);
            }
        });
    };

    $.submitEditCourseForm = function(t) {
        return $.submitForm(t, function(form, data){
            if (data.code == 1) {
                $.fetchCourseDropdown(data.new_nice_url);
                $(form).find("input[name=nice_url]").attr("value", data.new_nice_url);
                $(form).find('.ajax-message').htmlTimer(data.message, 5000);
            }
            else {
                $(form).find('.ajax-message').htmlTimer(data.message, 5000);
            }
        });
    }

    $.submitActionDeleteCourse = function () {
        var data = {
            action : 'delete_course'
        }
        var send = $.param(data);
        $.ajax({
            url : 'ajax/message-ajax.php',
            data : send,
            type : 'POST',
            dataType : 'html',
            success : function(message) {
                var c = confirm(message);
                if (c) {
                    var value = $('#course-dropdown option:selected').attr('value');
                    var data = {
                        action : 'delete_course',
                        nice_url : value
                    };
                    var send = $.param(data);
                    $("#course-dropdown").attr("disabled", "disabled");
                    $(".form-button").addClass("disabled");
                    $(".form-button").attr("disabled","disabled");
                    $("#course-dropdown").parent().find('.ajax-load').show();

                    $.ajax({
                        url : 'ajax/course-ajax.php',
                        type : "POST",
                        data : send,
                        dataType: 'json',
                        success : function(data) {
                            $("#course-dropdown").removeAttr('disabled');
                            $(".form-button").removeClass("disabled");
                            $(".form-button").removeAttr('disabled');
                            $("#course-dropdown").parent().find('.ajax-load').hide();

                            if (data.code == 1) {
                                $.fetchCourseDropdown(0);
                                $("#course-dropdown").parent().find('.ajax-message').htmlTimer(data.message, 5000);
                            }
                            else {
                                $("#course-dropdown").parent().find('.ajax-message').htmlTimer(data.message, 5000);
                            }
                        },
                        error : function() {
                            $("#course-dropdown").removeAttr('disabled');
                            $(".form-button").removeClass("disabled");
                            $(".form-button").removeAttr('disabled');
                            $("#course-dropdown").parent().find('.ajax-load').hide();
                        }
                    });
                }
            }
        });
    };

    $.submitDocumentAddForm = function() {
        $("#document-add").validate({
            errorClass: 'form-error',
            errorElement: 'div',
            submitHandler: function(form) {
                $(form).ajaxSubmit({
                    beforeSubmit: function(formParams, jqForm, options) {
                        //Disable button
                        $(".form-button").addClass("disabled");
                        $(".form-button").attr("disabled","disabled");

                        //Load image
                        $(jqForm).find(".ajax-load").show();
                        this.form = jqForm;
                        return true;
                    },
                    success: function(responseText, statusText) {
                        //Disable button
                        $(".form-button").removeClass("disabled");
                        $(".form-button").removeAttr("disabled");

                        //Load image
                        $(this.form).find(".ajax-load").hide();
                        $.fetchDocumentsTable(".jtable");
                        $(this.form).find('.ajax-message').htmlTimer(responseText, 5000);
                        $(this.form).resetForm();
                    }
                });
            }
        }); 
    };

    $.submitDocumentEditForm = function(t) {
        return $.submitForm(t, function(form, data){
            if (data.code == 1) {
                $.fetchDocumentsTable(".jtable");
            }
            else {
                $(form).find('.ajax-message').htmlTimer(data.message, 5000);
            }
        })
    };

    $.submitActionDocumentDelete = function() {
        var numSelected = $(".jtable tr.selected").length;
        if (numSelected < 1) {
            return false;
        }
        
        var data = {
            action : 'delete_document',
            num : numSelected
        }
        var send = $.param(data);
        $.ajax({
            url : 'ajax/message-ajax.php',
            type : 'POST',
            data : send,
            dataType : 'html',
            success : function(message) {
                var c = confirm(message);
                if (c) {
                    var nice_url = $('#course-dropdown option:selected').attr('value');
                    data = [
                        {
                            name : 'action',
                            value : 'delete_document'
                        },
                        {
                            name : 'nice_url',
                            value : nice_url
                        }
                    ];
                    $(".jtable tr.selected").each(function(){
                        data.push(
                            {
                                name : $(this).find('.old-name-hidden').attr('name'),
                                value : $(this).find('.old-name-hidden').attr('value')
                            }
                        );
                    });
                    send = $.param(data);
                    //Disable button
                    $(".form-button").addClass("disabled");
                    $(".form-button").attr("disabled","disabled");

                    //Load image
                    $('.document-control').find(".ajax-load").show();
                    $.ajax({
                        url : 'ajax/course-ajax.php',
                        type : 'POST',
                        data : send,
                        dataType : 'html',
                        success : function(data) {
                            //Disable button
                            $(".form-button").removeClass("disabled");
                            $(".form-button").removeAttr("disabled");

                            //Load image
                            $('.document-control').find(".ajax-load").hide();
                            $.fetchDocumentsTable(".jtable");
                            $('.document-delete-feedback').find('.ajax-message').htmlTimer(data, 5000);
                        }
                    });
                    
                }
            }
        });

        return false;
    };

})(jQuery);

$(function() {
    $('#course-dropdown').change(function(){
        $.fetchEditCourseForm();
        $.fetchDocumentsTable(".jtable");
        $.fetchDocumentAddForm();
    });

    $('.input-lang-changer').change(function(){
        $.inputLangChange(this);
    });

    $('.form-toggle').click(function(){
        $.showForm(this);
        return false;
    });

    $('.form-button[type=reset]').click(function(){
        $.hideForm(this);
    });

    $('.course-add-ajax-submit').click(function(){
        $.submitAddCourseForm(this);
    });

    $(".course-edit-ajax-submit").click(function(){
        return $.submitEditCourseForm(this);
    });

    $(".course-delete").click(function(){
        $.submitActionDeleteCourse();
        return false;
    });

    $(".jtable").tablesorter();
    
    $.fetchDocumentsTable(".jtable");

    $(".document-add-ajax-submit").click(function(){
        return $.submitDocumentAddForm();
    });

    $(".document-delete").click(function(){
        $.submitActionDocumentDelete();
        return false;
    });
    
});


